import { useRoute, useRouter as baseRouter } from "vue-router";
import { useLoadingBar } from "naive-ui";
import { useI18n, UseI18nOptions } from "vue-i18n";

export function useRouter<Options extends UseI18nOptions = UseI18nOptions>() {
  const router = baseRouter();
  const route = useRoute();
  const loadingBar = useLoadingBar();

  async function toName(name: string, params?: any) {
    console.log(`[toName] ${name}`);
    loadingBar.start();
    if (router.hasRoute(name)) {
      await router.push({
        name: name,
        params: params,
      });
    } else {
      console.log(`[toName] 没有找到 name`);
      await router.push({
        path: `/${name}`,
      });
    }
    loadingBar.finish();
  }

  // const { t } = useI18n<Options>();
  // function translate(name: string) {
  //   return t(`router.${name}`);
  // }

  return {
    router,
    route,
    toName,
    // translate,
  };
}
